import requests
import json

API_KEY = "a5eJYj6IyLgR7peHo99aUdbA"
SECRET_KEY = "snUFdCv6nm9T3qWfQizQStjE1mfGEdaC"


def main():
    problem = input('请输入问题：')
    url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-3.5-8k-0205?access_token=" + get_access_token()

    payload = json.dumps({
        "messages": [
            {
                "role": "user",
                "content": problem
            },
        ],
        "disable_search": False,
        "enable_citation": False
    })
    headers = {
        'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)
    print('')
    z=response.json()
    print('返回结果：',z.get('result'),sep="\n")
def get_access_token():
    """
    使用 AK，SK 生成鉴权签名（Access Token）
    :return: access_token，或是None(如果错误)
    """
    url = "https://aip.baidubce.com/oauth/2.0/token"
    params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
    access_token=str(requests.post(url, params=params).json().get("access_token"))
    print('access_token:',access_token)
    return access_token;


if __name__ == '__main__':
    main()
